/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.servlets;

import com.google.gson.Gson;
import com.oea.hibernate.entities.Answer;
import com.oea.hibernate.entities.Question;
import com.oea.hibernate.entities.Result;
import com.oea.hibernate.entities.Student;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author unnati
 */
public class AnalysisAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            String action = request.getParameter("action");
           // Long testid = (Long) request.getSession().getAttribute("testid");
           
            Gson gson=new Gson();
            
            if (action.equals("topic_class"))
            {
                Long testid=Long.parseLong(request.getParameter("test"));
                Long divid=Long.parseLong(request.getParameter("divid"));
                
                HashMap<Long, String> topic_division_score_map = new HashMap<>();
                List<Answer> li_answer = Answer.GetAllAnswerOfDivision(testid, divid);

                for (Answer answer : li_answer) 
                {
                    Long qid = answer.getQid();
                    Long topicid = Question.GetTopicid(qid);
                    String topic = topic_division_score_map.get(topicid);
                    String ans = Question.GetAnswer(qid);
                    if (topic != null) 
                    {
                        if (ans.equals(answer.getSanswer())) 
                        {
                            String[] part = topic.split("_");
                            int correct = Integer.parseInt(part[0]);
                            correct++;
                            int totalque = Integer.parseInt(part[1]);
                            totalque++;
                            String s = correct + "_" + totalque;
                            topic_division_score_map.put(topicid, s);
                        } 
                        else
                        {
                            String[] part = topic.split("_");
                            int correct = Integer.parseInt(part[0]);
                            int totalque = Integer.parseInt(part[1]);
                            totalque++;
                            String s = correct + "_" + totalque;
                            topic_division_score_map.put(topicid, s);
                        }

                    } 
                    else 
                    {
                        if (ans.equals(answer.getSanswer()))
                        {
                            topic_division_score_map.put(topicid, "1_1");
                        }
                        else 
                        {
                            topic_division_score_map.put(topicid, "0_1");
                        }
                    }
                }
                HashMap<Long,Float> analysis_topic_class=new HashMap<>();
                
                for (Map.Entry<Long,String> analysis : topic_division_score_map.entrySet())
                {
                     String s=analysis.getValue();
                     String[] part=s.split("_");
                     float f= (Float.parseFloat(part[0])/Float.parseFloat(part[1]))*100;
                     String fl=String.valueOf(f);
                     analysis_topic_class.put(analysis.getKey(),f);
                }
                String json=gson.toJson(analysis_topic_class);
                System.out.println(json);
                json=json.replace("{", "[");
                json=json.replace("}", "]");
                System.out.println(json);
                json=json.replace(",", "],[");
                System.out.println(json);
                json=json.replace(":", ",");
                System.out.println(json);
                json= json.replace('"', '\'');
                System.out.println(json);
                out.println(json);
             
            }
            
            else if(action.equals("topic_student"))
            {
                 Long testid=Long.parseLong(request.getParameter("test"));
                 System.out.println(testid);
                Long studid=Long.parseLong(request.getParameter("studid"));
                HashMap<Long,String> topic_student_score_map=new HashMap<>();
                
                List<Answer> li_answers=Answer.GetAllAnswerOfStudent(testid, studid);
                System.out.println(li_answers.size());
                for (Answer answer : li_answers) 
                {   
                     Long qid = answer.getQid();
                    Long topicid = Question.GetTopicid(qid);
                    String topic = topic_student_score_map.get(topicid);
                    String ans = Question.GetAnswer(qid);
                    if (topic != null) 
                    {
                        if (ans.equals(answer.getSanswer())) 
                        {
                            String[] part = topic.split("_");
                            int correct = Integer.parseInt(part[0]);
                            correct++;
                            int totalque = Integer.parseInt(part[1]);
                            totalque++;
                            String s = correct + "_" + totalque;
                            topic_student_score_map.put(topicid, s);
                        } 
                        else
                        {
                            String[] part = topic.split("_");
                            int correct = Integer.parseInt(part[0]);
                            int totalque = Integer.parseInt(part[1]);
                            totalque++;
                            String s = correct + "_" + totalque;
                            topic_student_score_map.put(topicid, s);
                        }

                    } 
                    else 
                    {
                        if (ans.equals(answer.getSanswer()))
                        {
                            topic_student_score_map.put(topicid, "1_1");
                        }
                        else 
                        {
                            topic_student_score_map.put(topicid, "0_1");
                        }
                    }
                }
                HashMap<Long,String> analysis_topic_class=new HashMap<>();
                for (Map.Entry<Long,String> analysis : topic_student_score_map.entrySet())
                {
                     String s=analysis.getValue();
                     String[] part=s.split("_");
                     float f= (Float.parseFloat(part[0])/Float.parseFloat(part[1]))*100;
                     String fl=String.valueOf(f);
                     analysis_topic_class.put(analysis.getKey(),fl);
                }
                String json=gson.toJson(analysis_topic_class);
                out.println(json);
                System.out.println(json);
                    
                System.out.println(analysis_topic_class);
                
            }
            else if(action.equals("exam_student"))
            
            {
                Long examid=Long.parseLong(request.getParameter("exam"));
                Long studid=Long.parseLong(request.getParameter("studid"));
                HashMap<Long,String> exam_student_score_map=new HashMap<>();
                List<Result> li_result=Result.GetResultByTestOfStudent(examid, studid);
                
                for (Result result : li_result) 
                {
                     Long tid = result.getTid();
                    float cans = result.getCanswer();
                    float wans = result.getWanswer();
                    float skipans=result.getSkipans();
                    float totalque = cans + wans+skipans;
                    String testid=exam_student_score_map.get(tid);
                    if(testid!=null)
                    {
                        String[] part=testid.split("_");
                        float correctans=Float.parseFloat(part[0]);
                        float wrongans=Float.parseFloat(part[1]);
                        float skippedans=Float.parseFloat(part[2]);
                        cans=cans+correctans;
                        wans=wans+wrongans;
                        skipans=skipans+skippedans;
                        totalque=cans+wans+skipans;
                        exam_student_score_map.put(tid, cans+"_"+wans+"_"+skipans);
                    }
                    else
                    {
                       
                        exam_student_score_map.put(tid, cans+"_"+wans+"_"+skipans);
                    }
                   
                }
                    HashMap<Long,Float> analysis_exam_student=new HashMap<>();
                     for (Map.Entry<Long,String> analysis : exam_student_score_map.entrySet())
                {
                     String s=analysis.getValue();
                     String[] part=s.split("_");
                     float totalquestion=Float.parseFloat(part[0])+Float.parseFloat(part[1])+Float.parseFloat(part[2]);
                     float f= (Float.parseFloat(part[0])/totalquestion)*100;
                     String fl=String.valueOf(f);
                     analysis_exam_student.put(analysis.getKey(),f);
                }
                  
                
            System.out.println(analysis_exam_student);
            }
            else if(action.equals("exam_class"))
            {
                System.out.println("ronak");
                Long examid=Long.parseLong(request.getParameter("exam"));
                Long divid=Long.parseLong(request.getParameter("divid"));
                HashMap<Long,String> exam_class_score_map=new HashMap<>();
                List<Result> li_result=Result.GetResultByTestOfClass(examid, divid);
                
                for (Result result : li_result) 
                {
                   // System.out.println(result.getId());
                    Long tid = result.getTid();
                    float cans = result.getCanswer();
                    float wans = result.getWanswer();
                    float skipans=result.getSkipans();
                    float totalque = cans + wans+skipans;
                    String testid=exam_class_score_map.get(tid);
                    if(testid!=null)
                    {
                        String[] part=testid.split("_");
                        float correctans=Float.parseFloat(part[0]);
                        float wrongans=Float.parseFloat(part[1]);
                        float skippedans=Float.parseFloat(part[2]);
                        cans=cans+correctans;
                        wans=wans+wrongans;
                        skipans=skipans+skippedans;
                        totalque=cans+wans+skipans;
                        exam_class_score_map.put(tid, cans+"_"+wans+"_"+skipans);
                    }
                    else
                    {
                       
                        exam_class_score_map.put(tid, cans+"_"+wans+"_"+skipans);
                    }
                   
                }
                    HashMap<Long,Float> analysis_exam_class=new HashMap<>();
                     for (Map.Entry<Long,String> analysis : exam_class_score_map.entrySet())
                {
                     String s=analysis.getValue();
                     String[] part=s.split("_");
                     float totalquestion=Float.parseFloat(part[0])+Float.parseFloat(part[1])+Float.parseFloat(part[2]);
                     float f= (Float.parseFloat(part[0])/totalquestion)*100;
                     String fl=String.valueOf(f);
                     analysis_exam_class.put(analysis.getKey(),f);
                }
                     System.out.println(analysis_exam_class);
            }
            
            else if(action.equals("test_progress_student"))
            {
                Long stuid=Long.parseLong(request.getParameter("stuid"));
                Long subid=Long.parseLong(request.getParameter("subid"));
                
                HashMap<String,Float> test_student_progress_map=new HashMap<>();
                List<Result>li_result=Result.StudentProgressByTest(stuid, subid);
               
                
                for (Result result : li_result) 
                {
                    
                    result.setTest();
                  //  Long testid=result.getTid();
                    String testname=result.getTest().getName();
                    float cans=result.getCanswer();
                    float wans=result.getWanswer();
                    float skipans=result.getSkipans();
                    float totalque=cans+wans+skipans;
                    
                    float perc=(cans/totalque)*100;
                    test_student_progress_map.put(testname, perc);
                    
                }
                
                System.out.println(test_student_progress_map);
                
            }
            else if(action.equals("test_progress_class"))
            {
                Long divid=Long.parseLong(request.getParameter("divid"));
                Long subid=Long.parseLong(request.getParameter("subid"));
                
                HashMap<String,String> test_class_progress_map=new HashMap<>();
                List<Result> li_result=Result.ClassProgressByTest(divid, subid);
                System.out.println(li_result.size());
                for (Result result : li_result) 
                {
                    result.setTest();
                  //  System.out.println(result.getId()+"and"+ result.getTest().getName());
                     String testname=result.getTest().getName();
                     String test=test_class_progress_map.get(testname);
                     if(test!=null)
                     {
                         String[] part=test.split("_");
                         float cans=Float.parseFloat(part[0])+result.getCanswer();
                         float wans=Float.parseFloat(part[1])+result.getWanswer();
                         float skipans=Float.parseFloat(part[2]+result.getSkipans());
                         test_class_progress_map.put(testname, cans+"_"+wans+"_"+skipans);
                         
                     }
                     else
                     {
                         float cans=result.getCanswer();
                         float wans=result.getWanswer();
                         float skipans=result.getSkipans();
                         test_class_progress_map.put(testname, cans+"_"+wans+"_"+skipans);
                     }
                   
                   
                }
                HashMap<String,Float> analysis_calss_test_progress =new HashMap<>();
                
                for(Map.Entry<String,String> analysis : test_class_progress_map.entrySet())
                {
                    
                    String testname=analysis.getKey();
                    
                    String result=analysis.getValue();
                    
                    String[] part = result.split("_");
                    float totalque=Float.parseFloat(part[0])+Float.parseFloat(part[1])+Float.parseFloat(part[2]);
                    float perc=(Float.parseFloat(part[0])/totalque)*100;
                    
                    analysis_calss_test_progress.put(testname, perc);
                }
                System.out.println(analysis_calss_test_progress);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
